package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.*;
import com.hualude.roadsign.service.HighLeverFaultService;
import com.hualude.roadsign.utils.PushUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by xu_jiaHui on 2018/3/24.
 */
@Service
public class HighLeverFaultServiceImpl implements HighLeverFaultService {
    @Resource
    private HighLeverFaultRepository highLeverFaultRepository;
    @Resource
    private HighLeverRepository highLeverRepository;
    @Resource
    private UserRepository userRepository;
    @Resource
    private OrgRepository orgRepository;
    @Resource
    private PushHistoryRepository pushHistoryRepository;

    private String strUserIds;

    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public DTResponseDto getHighLeverReportFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId, Integer orgId) throws Exception {
        Integer count;
        List<DTHighLeverFaultDto> dto4DTs;
        count = highLeverFaultRepository.getHighLeverReportFaultCount4DT(actorId, userId, orgId, dtRequestDto.getSearch());
        dto4DTs = highLeverFaultRepository.getHighLeverReportFault4DT(dtRequestDto, actorId, userId, orgId);
        for (DTHighLeverFaultDto dto : dto4DTs) {
            dto.setHighLeverCheckerName(userRepository.getUserNameByUserId(dto.getHighLeverCheckerId()));
        }
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        return dtResponseDto;
    }

    public HighLeverFaultDto getHighLeverInfo2Fault(Integer highLeverId) throws Exception {
        HighLeverFaultDto highLeverFaultDto;
        highLeverFaultDto = highLeverRepository.getHighLeverInfo2Fault(highLeverId);
        highLeverFaultDto.setHighLeverCreatorName(userRepository.getUserNameByUserId(highLeverFaultDto.getHighLeverCreatorId()));
        highLeverFaultDto.setHighLeverOrgName(orgRepository.getMainOrgName(highLeverFaultDto.getHighLeverOrgId()));
        highLeverFaultDto.setHighLeverMainOrgName(orgRepository.getMainOrgName(highLeverFaultDto.getHighLeverMainOrgId()));
        highLeverFaultDto.setHighLeverId(highLeverId);
        return highLeverFaultDto;
    }

    public HighLeverFaultDto getHighLeverFaultInfo2Fault(Integer highLeverId, Integer highLeverFaultId) throws Exception {
        HighLeverFaultDto highLeverFaultDto;
        highLeverFaultDto = highLeverRepository.getHighLeverInfo2Fault(highLeverId);
        HighLeverFaultDto highLeverFault = highLeverFaultRepository.getHighLeverFaultInfo2Fault(highLeverFaultId);
        highLeverFault.setHighLeverProvince(highLeverFaultDto.getHighLeverProvince());
        highLeverFault.setHighLeverCity(highLeverFaultDto.getHighLeverCity());
        highLeverFault.setHighLeverArea(highLeverFaultDto.getHighLeverArea());
        highLeverFault.setHighLeverRoadName(highLeverFaultDto.getHighLeverRoadName());
        highLeverFault.setHighLeverStartRoad(highLeverFaultDto.getHighLeverStartRoad());
        highLeverFault.setHighLeverEndRoad(highLeverFaultDto.getHighLeverEndRoad());
        highLeverFault.setHighLeverLongitude(highLeverFaultDto.getHighLeverLongitude());
        highLeverFault.setHighLeverLatitude(highLeverFaultDto.getHighLeverLatitude());
        highLeverFault.setHighLeverOrgId(highLeverFaultDto.getHighLeverOrgId());
        highLeverFault.setHighLeverMainOrgId(highLeverFaultDto.getHighLeverMainOrgId());
        UserDto userDto = userRepository.getUserById(highLeverFault.getHighLeverFaultCreatorId());
        highLeverFault.setHighLeverFaultCreatorName(userDto.getUserName());
        highLeverFault.setHighLeverFaultCreatorPhone(userDto.getUserMobile());
        return highLeverFault;
    }

    @Transactional
    public Integer updateHighLeverFault(Integer highLeverFaultId, Integer highLeverFaultLevel, Integer highLeverFaultType, String highLeverFaultPhoto, String highLeverFaultReason, Double highLeverMoney) throws Exception {
        return highLeverFaultRepository.updateHighLeverFault(highLeverFaultId, highLeverFaultLevel, highLeverFaultType, highLeverFaultPhoto, highLeverFaultReason, highLeverMoney);
    }

    @Transactional
    public Integer deleteHighLeverFaultByIds(String highLeverFaultIds) throws Exception {
        String[] faultIdArray = highLeverFaultIds.split(",");
        int i = 0;
        for (i = 0; i < faultIdArray.length; i++) {
            highLeverFaultRepository.deleteHighLeverFaultByIds(Integer.parseInt(faultIdArray[i]));
        }
        return i;
    }

    public List<HighLeverFaultDto> getHighLeverCoordinate4Map(Integer orgId, Integer actorId) throws Exception {
        return highLeverRepository.getHighLeverCoordinate4Map(orgId, actorId);
    }

    public List<HighLeverFaultDto> getHighLeverByXY(String longitude, String latitude) throws Exception {
        return highLeverRepository.getHighLeverByXY(longitude, latitude);
    }

    public DTResponseDto getHighLeverFaultReport4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId, String highLeverRoadName, String highLeverStartRoad, String highLeverEndRoad) throws Exception {
        if (highLeverStartRoad.equals("") && highLeverEndRoad.equals("")) {
            return new DTResponseDto(dtRequestDto.getDraw(), 0, 0, "");
        } else {
            Integer count = highLeverRepository.getHighLeverFaultReportCount4DT(dtRequestDto.getSearch(), actorId, orgId, highLeverRoadName, highLeverStartRoad, highLeverEndRoad);
            List<DTHighLeverFaultDto> dto4DTs = highLeverRepository.getHighLeverFaultReport4DT(dtRequestDto, actorId, orgId, highLeverRoadName, highLeverStartRoad, highLeverEndRoad);
            for (DTHighLeverFaultDto dto : dto4DTs) {
                List<HighLeverFaultDto> highLeverFaultDtos = highLeverFaultRepository.getHighLeverFaultIsReport(dto.getHighLeverId());
                Integer result = 1;
                if (highLeverFaultDtos.size() != 0) {
                    Integer mainCheck = highLeverFaultDtos.get(0).getHighLeverMainCheck();
                    Integer check = highLeverFaultDtos.get(0).getHighLeverCheck();
                    Integer status = highLeverFaultDtos.get(0).getHighLeverMaintenanceStatus();
                    if (status == 2) {
                        result = 1;//维修完成
                    } else if (mainCheck == -1 || check == -1) {
                        result = 1;//
                    } else {
                        result = 0;
                    }
                }
                dto.setReportStatus(result);
            }
            DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
            return dtResponseDto;
        }
    }


    public Integer getHighLeverFaultIsReport(Integer highLeverId) throws Exception {
        List<HighLeverFaultDto> highLeverFaultDtos = highLeverFaultRepository.getHighLeverFaultIsReport(highLeverId);
        Integer result = 1;
        if (highLeverFaultDtos.size() != 0) {
            Integer mainCheck = highLeverFaultDtos.get(0).getHighLeverMainCheck();
            Integer check = highLeverFaultDtos.get(0).getHighLeverCheck();
            Integer status = highLeverFaultDtos.get(0).getHighLeverMaintenanceStatus();
            if (status == 2) {
                result = 1;//维修完成
            } else if (mainCheck == -1 || check == -1) {
                result = 1;//
            } else {
                result = 0;
            }
        }
        return result;
    }


    public Integer reportHighLeverFault(final Integer highLeverId, Integer highLeverFaultCreatorId, Integer highLeverFaultLevel, Integer highLeverFaultType,
                                    String highLeverFaultPhoto, String highLeverFaultReason, Double highLeverMoney, final Integer highLeverOrgId, Integer highLeverMainOrgId) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        if (highLeverMoney == null)
            highLeverMoney = 0.0;
        HighLeverFaultDto insertDto = new HighLeverFaultDto();
        insertDto.setHighLeverId(highLeverId);
        insertDto.setHighLeverFaultCreatorId(highLeverFaultCreatorId);
        insertDto.setHighLeverFaultLevel(highLeverFaultLevel);
        insertDto.setHighLeverFaultType(highLeverFaultType);
        insertDto.setHighLeverFaultTime(df.format(new Date()));
        insertDto.setHighLeverFaultPhoto(highLeverFaultPhoto);
        insertDto.setHighLeverFaultReason(highLeverFaultReason);
        insertDto.setHighLeverMoney(highLeverMoney);
        insertDto.setHighLeverOrgId(highLeverOrgId);
        insertDto.setHighLeverMainOrgId(highLeverMainOrgId);

        List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(highLeverOrgId, 3);
        final String msg = "窨井 NO：" + highLeverId + " 待审核";
        strUserIds = "";
        for (UserDto userDto : userDtos) {
            strUserIds += userDto.getUserId().toString() + ",";
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (!strUserIds.equals("")) {
                        strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                        PushUtil.sendPushToUser(strUserIds, msg);
                        PushHistoryDto pushHistoryDto = new PushHistoryDto(highLeverOrgId, 3, msg, formatter.format(new Date()),9);
                        pushHistoryRepository.save(pushHistoryDto.toBean());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        System.out.println("价格为: "+highLeverMoney);
        return highLeverFaultRepository.save(insertDto.toBean()).getHighLeverId();
    }

    public List<String> getStartRoad(String roadName, Integer actorId, Integer orgId) throws Exception {
        return highLeverRepository.getStartRoad(roadName, actorId, orgId);
    }

    public List<String> getSectionByStartRoad(String roadName, Integer actorId, Integer orgId) throws Exception {
        return highLeverRepository.getSectionByStartRoad(roadName, actorId, orgId);
    }

    public List<HighLeverFaultDto> getHighLeverFaultList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception {
        return highLeverFaultRepository.getHighLeverFaultList4App(orgId, actorIds, faultStatus, count, size);
    }
}
